package Views;

import Controllers.LocationController;
import Controllers.RoundController;
import Models.EmptyValueException;
import Models.Location;
import Models.Round;
import Views.TableModels.RoundTableModel;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;

public class LocationPanel extends ViewAbstract<Location>
{
    public LocationPanel(SearchableTabbedPane parent)
	{
		initComponents();
		this.controller = new LocationController();
		this.tabbedPane = parent;
		roundTable.getSelectionModel().addListSelectionListener(this);
		roundTable.addMouseListener(new java.awt.event.MouseAdapter(){
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(roundTable.getSelectedRowCount() == 1)
					{
						showRoundBActionPerformed(null);
					}
				}
			}
		});
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationNoL = new javax.swing.JLabel();
        addressL = new javax.swing.JLabel();
        zipCodeL = new javax.swing.JLabel();
        cityL = new javax.swing.JLabel();
        routeNoL = new javax.swing.JLabel();
        mapL = new javax.swing.JLabel();
        longitudeL = new javax.swing.JLabel();
        LatitudeL = new javax.swing.JLabel();
        locationNoTF = new javax.swing.JTextField();
        addressTF = new javax.swing.JTextField();
        zipCodeTF = new javax.swing.JTextField();
        cityTF = new javax.swing.JTextField();
        routeNoTF = new javax.swing.JTextField();
        longitudeTF = new javax.swing.JTextField();
        latitudeTF = new javax.swing.JTextField();
        searchTF = new javax.swing.JTextField();
        addRoundB = new javax.swing.JButton();
        removeRoundB = new javax.swing.JButton();
        showRoundB = new javax.swing.JButton();
        searchB = new javax.swing.JButton();
        showAllB = new javax.swing.JButton();
        newB = new javax.swing.JButton();
        editB = new javax.swing.JButton();
        deleteB = new javax.swing.JButton();
        cancelB = new javax.swing.JButton();
        saveB = new javax.swing.JButton();
        Sep1 = new javax.swing.JSeparator();
        Sep2 = new javax.swing.JSeparator();
        Sep3 = new javax.swing.JSeparator();
        roundSP = new javax.swing.JScrollPane();
        roundTable = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(600, 420));

        locationNoL.setText("Lokations nr.:");
        locationNoL.setName("locationNoL"); // NOI18N

        addressL.setText("Adresse:");
        addressL.setName("addressL"); // NOI18N

        zipCodeL.setText("Post nr.:");
        zipCodeL.setName("zipCodeL"); // NOI18N

        cityL.setText("By:");
        cityL.setName("cityL"); // NOI18N

        routeNoL.setText("Rute nr.:");
        routeNoL.setName("routeNoL"); // NOI18N

        mapL.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Views/resources/locationMap.JPG"))); // NOI18N
        mapL.setName("mapL"); // NOI18N

        longitudeL.setText("Breddegrad:");
        longitudeL.setName("longitudeL"); // NOI18N

        LatitudeL.setText("Længdegrad:");
        LatitudeL.setName("LatitudeL"); // NOI18N

        locationNoTF.setEnabled(false);
        locationNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        locationNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        locationNoTF.setName("locationNoTF"); // NOI18N
        locationNoTF.setNextFocusableComponent(addressTF);
        locationNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        addressTF.setEnabled(false);
        addressTF.setMaximumSize(new java.awt.Dimension(200, 20));
        addressTF.setMinimumSize(new java.awt.Dimension(200, 20));
        addressTF.setName("addressTF"); // NOI18N
        addressTF.setNextFocusableComponent(zipCodeTF);
        addressTF.setPreferredSize(new java.awt.Dimension(200, 20));

        zipCodeTF.setEnabled(false);
        zipCodeTF.setMaximumSize(new java.awt.Dimension(200, 20));
        zipCodeTF.setMinimumSize(new java.awt.Dimension(200, 20));
        zipCodeTF.setName("zipCodeTF"); // NOI18N
        zipCodeTF.setNextFocusableComponent(cityTF);
        zipCodeTF.setPreferredSize(new java.awt.Dimension(200, 20));

        cityTF.setEnabled(false);
        cityTF.setMaximumSize(new java.awt.Dimension(200, 20));
        cityTF.setMinimumSize(new java.awt.Dimension(200, 20));
        cityTF.setName("cityTF"); // NOI18N
        cityTF.setNextFocusableComponent(routeNoTF);
        cityTF.setPreferredSize(new java.awt.Dimension(200, 20));

        routeNoTF.setEnabled(false);
        routeNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        routeNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        routeNoTF.setName("routeNoTF"); // NOI18N
        routeNoTF.setNextFocusableComponent(addRoundB);
        routeNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        longitudeTF.setEnabled(false);
        longitudeTF.setMaximumSize(new java.awt.Dimension(200, 20));
        longitudeTF.setMinimumSize(new java.awt.Dimension(200, 20));
        longitudeTF.setName("longitudeTF"); // NOI18N
        longitudeTF.setPreferredSize(new java.awt.Dimension(200, 20));

        latitudeTF.setEnabled(false);
        latitudeTF.setMaximumSize(new java.awt.Dimension(200, 20));
        latitudeTF.setMinimumSize(new java.awt.Dimension(200, 20));
        latitudeTF.setName("latitudeTF"); // NOI18N
        latitudeTF.setPreferredSize(new java.awt.Dimension(200, 20));

        searchTF.setMaximumSize(new java.awt.Dimension(200, 20));
        searchTF.setMinimumSize(new java.awt.Dimension(200, 20));
        searchTF.setName("searchTF"); // NOI18N
        searchTF.setPreferredSize(new java.awt.Dimension(200, 20));

        addRoundB.setText("Tilføj");
        addRoundB.setEnabled(false);
        addRoundB.setName("addRoundB"); // NOI18N
        addRoundB.setNextFocusableComponent(removeRoundB);
        addRoundB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRoundBActionPerformed(evt);
            }
        });

        removeRoundB.setText("Fjern");
        removeRoundB.setEnabled(false);
        removeRoundB.setName("removeRoundB"); // NOI18N
        removeRoundB.setNextFocusableComponent(newB);
        removeRoundB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeRoundBActionPerformed(evt);
            }
        });

        showRoundB.setText("Vis");
        showRoundB.setEnabled(false);
        showRoundB.setName("showRoundB"); // NOI18N
        showRoundB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showRoundBActionPerformed(evt);
            }
        });

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        showAllB.setText("Vis Alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        newB.setText("Ny");
        newB.setName("newB"); // NOI18N
        newB.setNextFocusableComponent(editB);
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        editB.setText("Rediger");
        editB.setEnabled(false);
        editB.setName("editB"); // NOI18N
        editB.setNextFocusableComponent(cancelB);
        editB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBActionPerformed(evt);
            }
        });

        deleteB.setText("Slet");
        deleteB.setEnabled(false);
        deleteB.setName("deleteB"); // NOI18N
        deleteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBActionPerformed(evt);
            }
        });

        cancelB.setText("Annullér");
        cancelB.setEnabled(false);
        cancelB.setName("cancelB"); // NOI18N
        cancelB.setNextFocusableComponent(saveB);
        cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBActionPerformed(evt);
            }
        });

        saveB.setText("Gem");
        saveB.setEnabled(false);
        saveB.setName("saveB"); // NOI18N
        saveB.setNextFocusableComponent(locationNoTF);
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        Sep1.setName("Sep1"); // NOI18N

        Sep2.setName("Sep2"); // NOI18N

        Sep3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        Sep3.setName("Sep3"); // NOI18N
        Sep3.setPreferredSize(new java.awt.Dimension(5, 23));

        roundSP.setName("roundSP"); // NOI18N

        roundTable.setModel(new RoundTableModel());
        roundTable.setEnabled(false);
        roundTable.setName("roundTable"); // NOI18N
        roundSP.setViewportView(roundTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(longitudeL)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(roundSP, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(addRoundB)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(removeRoundB)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(showRoundB))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(addressL)
                                                .addComponent(zipCodeL)
                                                .addComponent(cityL)
                                                .addComponent(routeNoL)
                                                .addComponent(locationNoL))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(addressTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(locationNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(zipCodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(cityTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(routeNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(longitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(latitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(LatitudeL)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Sep1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchB))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(mapL)
                                .addGap(21, 21, 21))))
                    .addComponent(Sep2, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(showAllB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
                        .addComponent(cancelB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchB)
                            .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Sep1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(mapL))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(locationNoL)
                            .addComponent(locationNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addressL)
                            .addComponent(addressTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(zipCodeL)
                            .addComponent(zipCodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cityL)
                            .addComponent(cityTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(routeNoL)
                            .addComponent(routeNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(longitudeL)
                            .addComponent(longitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(LatitudeL)
                            .addComponent(latitudeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(roundSP, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addRoundB)
                            .addComponent(removeRoundB)
                            .addComponent(showRoundB))
                        .addGap(2, 2, 2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Sep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(showAllB)
                    .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(newB)
                        .addComponent(editB)
                        .addComponent(saveB)
                        .addComponent(cancelB)
                        .addComponent(deleteB)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void newBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newBActionPerformed
		clearComponents();
		changeComponentState(true);
		newOne = new Location();
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_newBActionPerformed

	private void editBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editBActionPerformed
		changeComponentState(true);
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_editBActionPerformed
	
	private void deleteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBActionPerformed
		if(confirmDeletion("lokationen"))
		{
			controller.delete(chosenOne.getLocationNo());
			clearComponents();
		}
	}//GEN-LAST:event_deleteBActionPerformed
	
	private void cancelBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBActionPerformed
		clearComponents();
		changeComponentState(false);
	}//GEN-LAST:event_cancelBActionPerformed
	
	private void saveBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBActionPerformed
		Location theOne = getTheOne();
		
		try
		{
			theOne.setData(addressTF.getText(), zipCodeTF.getText(), latitudeTF.getText(), longitudeTF.getText());
			Models.Route route = new Controllers.RouteController().find(Integer.parseInt(routeNoTF.getText()));
			theOne.setRoute(route);
			
			if(chosenOne == null)
			{
				controller.insert(theOne);
			}
			else
			{
				controller.update(theOne, "locationNo = "+theOne.getLocationNo());
			}
		}
		catch (EmptyValueException e)
		{
			informAboutEmptyFields();
			return;
		}
		catch (NumberFormatException e)
		{
			informAboutWrongInput("Postnummer skal være heltal, og længde- og breddegrad skal være to heltal med et punktum imellem.", "Forkert information");
			return;
		}

		informSavingWentOk("Lokationen");
		clearComponents();
		changeComponentState(false);
		newB.setEnabled(true);
	}//GEN-LAST:event_saveBActionPerformed

	private void searchBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBActionPerformed
		tabbedPane.search(TabIndex.Locations, searchTF.getText());
	}//GEN-LAST:event_searchBActionPerformed

	private void showAllBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllBActionPerformed
		tabbedPane.search(TabIndex.Locations, "*");
	}//GEN-LAST:event_showAllBActionPerformed

	private void addRoundBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRoundBActionPerformed
		if(picker == null)
		{
			picker = createPicker(TabIndex.Rounds);
			picker.addWindowListener(this);
			picker.setVisible(true);
		}
	}//GEN-LAST:event_addRoundBActionPerformed

	private void removeRoundBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeRoundBActionPerformed
		int roundNo = Integer.parseInt(roundTable.getValueAt(roundTable.getSelectedRow(), 0).toString());
		Round round = new RoundController().find(roundNo);
		removeRound(round);
	}//GEN-LAST:event_removeRoundBActionPerformed

	private void showRoundBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showRoundBActionPerformed
	{//GEN-HEADEREND:event_showRoundBActionPerformed
		int roundNo = Integer.parseInt(roundTable.getValueAt(roundTable.getSelectedRow(), 0).toString());
		tabbedPane.switchTabWithNo(TabIndex.Rounds, roundNo);
	}//GEN-LAST:event_showRoundBActionPerformed

	private void addRound(Round round)
	{
		Location theOne = getTheOne();

		theOne.addRound(round);
		refreshTable(theOne.getRounds());
	}

	private void removeRound(Round round)
	{
		Location theOne = getTheOne();

		theOne.removeRound(round);
		refreshTable(theOne.getRounds());
	}

	private void refreshTable(ArrayList<Round> rounds)
	{
		roundTable.setModel(new RoundTableModel(rounds));
	}

	protected void changeComponentState(boolean state)
	{
		addressTF.setEnabled(state);
		zipCodeTF.setEnabled(state);
		routeNoTF.setEnabled(state);
		longitudeTF.setEnabled(state);
		latitudeTF.setEnabled(state);
		roundTable.setEnabled(state);
		cancelB.setEnabled(state);
		saveB.setEnabled(state);
		addRoundB.setEnabled(state);
		setUnsavedState(state);
	}

	protected void populateFields()
	{
		locationNoTF.setText(String.valueOf(chosenOne.getLocationNo()));
		addressTF.setText(chosenOne.getAddress());
		zipCodeTF.setText(String.valueOf(chosenOne.getZipCode()));
		cityTF.setText(chosenOne.getCity());
		routeNoTF.setText(String.valueOf(chosenOne.getRouteNo()));
		longitudeTF.setText(String.valueOf(chosenOne.getCheckpoint().getLongitude()));
		latitudeTF.setText(String.valueOf(chosenOne.getCheckpoint().getLatitude()));
		roundTable.setModel(new RoundTableModel(chosenOne.getRounds()));
		newB.setEnabled(true);
		editB.setEnabled(true);
		deleteB.setEnabled(true);
	}

	protected void clearComponents()
	{
		chosenOne = null;
		locationNoTF.setText("");
		addressTF.setText("");
		zipCodeTF.setText("");
		cityTF.setText("");
		routeNoTF.setText("");
		longitudeTF.setText("");
		latitudeTF.setText("");
		roundTable.setModel(new RoundTableModel());
		newB.setEnabled(true);
		editB.setEnabled(false);
		deleteB.setEnabled(false);
	}

	protected void showNo(int no)
	{
		chosenOne = controller.find(no);
		chosenOne.fetchSubItems();
		populateFields();
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if(roundTable.getSelectedRowCount() == 1)
		{
			removeRoundB.setEnabled(true);
			showRoundB.setEnabled(true);
		}
		else
		{
			removeRoundB.setEnabled(false);
			showRoundB.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(picker != null)
		{
			Round round = (Round)picker.getTheChosenOne();
			if(round != null)
			{
				this.addRound(round);
			}
			this.releasePicker();
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LatitudeL;
    private javax.swing.JSeparator Sep1;
    private javax.swing.JSeparator Sep2;
    private javax.swing.JSeparator Sep3;
    private javax.swing.JButton addRoundB;
    private javax.swing.JLabel addressL;
    private javax.swing.JTextField addressTF;
    private javax.swing.JButton cancelB;
    private javax.swing.JLabel cityL;
    private javax.swing.JTextField cityTF;
    private javax.swing.JButton deleteB;
    private javax.swing.JButton editB;
    private javax.swing.JTextField latitudeTF;
    private javax.swing.JLabel locationNoL;
    private javax.swing.JTextField locationNoTF;
    private javax.swing.JLabel longitudeL;
    private javax.swing.JTextField longitudeTF;
    private javax.swing.JLabel mapL;
    private javax.swing.JButton newB;
    private javax.swing.JButton removeRoundB;
    private javax.swing.JScrollPane roundSP;
    private javax.swing.JTable roundTable;
    private javax.swing.JLabel routeNoL;
    private javax.swing.JTextField routeNoTF;
    private javax.swing.JButton saveB;
    private javax.swing.JButton searchB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JButton showAllB;
    private javax.swing.JButton showRoundB;
    private javax.swing.JLabel zipCodeL;
    private javax.swing.JTextField zipCodeTF;
    // End of variables declaration//GEN-END:variables
}